<template>
  <div class="app-container">
    <el-row>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-col :span="6">
          <el-form-item label="考试名称" prop="examName">
            <el-input v-model="queryParams.examName" placeholder="请输入考试名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>


        <el-col :span="6">
          <!--<el-form-item label="设备" prop="equipmentId">
          <el-select
            v-model="queryParams.equipmentId"
            placeholder="请选择设备"
            clearable
            @change="changeQuesBank"
          >
            <el-option
              v-for="item in listBank"
              :key="item.id"
              :label="item.equipmentName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>-->

        </el-col>
        <el-col :span="6"></el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="abc"
          v-hasPermi="['system:exam:add']">新增</el-button>
        <!-- @click="handleAdd" -->
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:exam:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:exam:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:exam:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考试名称" align="center" prop="examName" />

      <el-table-column align="center" label="考试时间">
        <template slot-scope="scope">
          {{ scope.row.startTime !== 'null' && scope.row.endTime !== 'null' ?
            scope.row.startTime + ' ~' + scope.row.endTime : '不限时' }}
        </template>
      </el-table-column>
      <el-table-column label="考试总分" align="center" prop="totalScore" />
      <el-table-column label="合格分数" align="center" prop="passScore" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:exam:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:exam:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listExam, getExam, delExam, addExam, updateExam } from "@/api/exam/exams";
import { listBank } from "@/api/exam/bank";
import { listQuestionBank } from "@/api/exam/questionBank";


export default {
  name: "Exam",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      listBank: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试表格数据
      examList: [],
      allBank: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examName: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getBankInfo();
    this.getList();
    this.$store.dispatch('titleName/INIT_NAME');
  },
  methods: {
    /** 查询考试列表 */
    getList() {
      this.loading = true;
      listExam(this.queryParams).then(response => {
        // console.log("考試管理顯示",response);
        this.examList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        examName: null,
        examDesc: null,
        type: null,
        password: null,
        duration: null,
        startTime: null,
        endTime: null,
        totalScore: null,
        passScore: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getBankInfo() {
      listBank(null).then((response) => {
        console.log(response.rows);
        this.listBank = response.rows;
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.$router.push('/updateExam/' + id)
      // getExam(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改考试";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    changeQuesBank() {
      this.getQuestionBank(this.queryParams.level)
    },
    //题库
    getQuestionBank(e) {
      listQuestionBank({ id: e }).then((res) => {
        console.log("题库显示", res);
        this.allBank = res.rows;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考试编号为"' + ids + '"的数据项？').then(function () {
        return delExam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download('system/exam/export', {
      //   ...this.queryParams
      // }, `exam_${new Date().getTime()}.xlsx`)
    },
    abc() {
      console.log(this.$router);
      this.$router.push('/addExam')
    }
  }
};
</script>
